package com.ww.demo.core.entity.response;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * @author liuqingwang
 * @date 2021/10/25 22:35
 */

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {
    private int code = 200;
    private String message;
    private T data;

    private static final Result instance = new Result();

    public static Result create() {
        try {
            return (Result) instance.clone();
        } catch (CloneNotSupportedException e) {
            return new Result();
        }
    }

    public static Result create(int code, String message, Object data) {
        Result result;
        try {
            result = (Result) instance.clone();
        } catch (CloneNotSupportedException e) {
            result = new Result();
        }
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static Result fail(int code) {
        return create(code, "操作失败！", null);
    }

    public static Result fail(int code, String message) {
        return create(code, message, null);
    }

    public static Result success() {
        return success(null);
    }

    public static Result success(Object data) {
        return create(200, "操作成功!", data);
    }

    public static Result success(String message, Object data) {
        return create(200, message, data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
